<?php

/**
 * main actions.
 *
 * @package    sf_sandbox
 * @subpackage main
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mainActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $univ_id = $this->getUser()->getGuardUser()->getUniversityId();
        $q = UniversityNewsTable::getInstance()->createQuery()->where('university_id = ?', $univ_id)->orWhere('university_id IS NULL')->andWhere('expiry_date > ?', date('Y-m-d'));
        $this->univ_news_count = $q->count();
        if ($this->univ_news_count > 0)
            $this->univ_news = $q->fetchArray();
    }

    public function executeError404(sfWebRequest $request) {
        
    }

    public function executeGet_faculty_for_university(sfWebRequest $request) {
        $objs = FacultyTable::getInstance()->findBy('university_id', $request->getParameter('id'));
        return $this->renderPartial('options', array('objs' => $objs));
    }

    public function executeLogedStd(sfWebRequest $request) {
        sfContext::getInstance()->getConfiguration()->loadHelpers(array("Asset", "Image", "I18N", "Url"));
        $this->user = $this->getUser()->getGuardUser();
        $username = $this->user->getUsername();
        $local_path = sfConfig::get('sf_web_dir') . '/uploads/profile_pictures/' . $username . '_thumb.jpg';
        if (file_exists($local_path)) {
            $this->ftime = filemtime($local_path);
            $this->path = get_resized_image('133', '140', $local_path);
        } else {
            $this->path = image_path('demo_upload.gif');
            $this->ftime = '';
        }
        $q3 = UserBalanceTable::getInstance()->createQuery()->where('user_id = ?', $this->user->getId());
        $q3count = $q3->count();
        if ($q3count === 0) {
            $this->balance = '0.00';
        } else {
            $bobj = $q3->fetchOne();
            $this->balance = $bobj->get('balance');
        }
        $user_id = $this->user->getId();
        if ($request->isMethod(sfWebRequest::POST)) {
            $courses = $request->getParameter('course', false);
            $instructors = $request->getParameter('instructor', false);
            UserDosTable::getInstance()->createQuery()->delete()->where('user_id = ?', $user_id)->execute();
            $entered_courses = array();
            $sub = 8;
            if (count($courses) > 0) {
                // charge the user
                $sc_obj = UserBalanceTable::getInstance()->findOneBy('user_id', $user_id);
                if (is_object($sc_obj)) {
                    $balance = $sc_obj->get('balance');
                    if ($balance >= 8) {
                        $sc_obj->set('balance', $balance - $sub);
                        $sc_obj->save();

                        foreach ($courses as $k => $one) {
                            if ($one == '' || $instructors[$k] == '' || in_array($one, $entered_courses))
                                continue;
                            $obj = new UserDos();
                            $obj->set('user_id', $user_id);
                            $obj->set('course_id', $one);
                            $obj->set('instructor_id', $instructors[$k]);
                            $obj->save();
                            $entered_courses[] = $one;
                        }
                        $this->getUser()->setFlash('notice', __('Successfully added your courses, you have been charged') . ' ' . $sub . __('JDs.'));
                        $this->redirect('main/LogedStd');
                    } else {
                        $this->getUser()->setFlash('error', __('Insufficient balance, you have to add') . ' ' . $sub . __('JDs.'));
                    }
                } else {
                    $this->getUser()->setFlash('error', __('Insufficient balance, you have to add') . ' ' . $sub . __('JDs.'));
                }
            }
        }

        $current_courses_q = UserDosTable::getInstance()
                ->createQuery()
                ->select('id, course_id, instructor_id')
                ->where("user_id = ?", $user_id);
        $this->current_courses = $current_courses_q->fetchArray();

        $cc_count = $current_courses_q->count();

        $course_instructor = array();

        $culture = $this->getUser()->getCulture();

        foreach ($this->current_courses as $one) {
            $data = InstructorTable::getInstance()
                    ->createQuery('i')
                    ->select('i.id, i.name_' . $culture . ' as name')
                    ->innerJoin('i.CourseFaculty cf')
                    ->where('cf.course_id = ?', $one['course_id'])
                    ->fetchArray();
            $course_instructor[$one['course_id']] = $data;
        }

        $this->course_instructor = $course_instructor;

        $this->disable_form = $cc_count > 0;
    }
}
